package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.GoodsDao;
import com.qf.xiaomi.pojo.TbGoodsKz;
import com.qf.xiaomi.pojo.TbGoodsType;
import com.qf.xiaomi.pojo.pageBean;
import com.qf.xiaomi.pojo.TbGoods;
import com.qf.xiaomi.utils.DButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    QueryRunner queryRunner = new QueryRunner(DButils.getdataSource());

    @Override
    public pageBean  selectPage(long id,pageBean page) {
        String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=? limit ?,?";
        String countSql="select count(0)from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=? ";
        int pyl=(page.getCurrentPage()-1)*page.getPageSize();
        try {
            List<TbGoods> goodsList = queryRunner.query(sql, new BeanListHandler<TbGoods>(TbGoods.class), id, pyl, page.getPageSize());

//            long count =  queryRunner.query(countSql, new ScalarHandler<>(), id);
//            page.setGoods(goodsList);
//            page.setTotalCount(count);
            Map<String, Object> resultMap = queryRunner.query(countSql, new MapHandler(), id);
            //设置当前查询到的集合数据
            page.setList(goodsList);
            //设置总条数, 总条数就是我们第二个sql的返回结果,通过totalSize作为key保存在map中
            page.setTotalSize((Long) resultMap.get("count(0)"));
            page.setTotalPage(((page.getTotalSize()+page.getPageSize()-1)/page.getPageSize()));
            return page;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public TbGoods selectByGoodsId(long goodsId) {
        String sql="select tg.*,tgz.name,tgz.pubdate,tgz.picture,tgz.price,tgz.star,tgz.intro,tgz.typeid from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tg.id=?";


        try {
            TbGoods goodsKz = queryRunner.query(sql, new BeanHandler<>(TbGoods.class), goodsId);
//            System.out.println(goodsKz);
            return  goodsKz;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TbGoods> selectByGoodsIds(List ids) {
        String StringBuffer ="";
        for (int i = 0; i < ids.size(); i++) {
            StringBuffer+="?";
            if(i<ids.size()-1){
                StringBuffer+=",";
            }
        }
        System.out.println(StringBuffer);
        String sql="select tg.*,tgz.name,tgz.pubdate,tgz.picture,tgz.price,tgz.star,tgz.intro,tgz.typeid from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tg.id in("+StringBuffer+")";
        List<TbGoods> goodsList = null;
        try {
            goodsList = queryRunner.query(sql, new BeanListHandler<>(TbGoods.class),ids.toArray());
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public TbGoodsKz findByPid(long pid) {
        String sql = "select * from tb_goods_kz where id=?";
        try {
            TbGoodsKz goods = queryRunner.query(sql, new BeanHandler<>(TbGoodsKz.class), pid);
            return  goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override

    public List<TbGoods> findGoods() {
        String sql = "select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id ";
        try {
            List<TbGoods> tbGoods = queryRunner.query(sql, new BeanListHandler<>(TbGoods.class));
            return tbGoods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TbGoodsType findByTid(Long typeid) {
        String sql = "select * from tb_goods_type where id=?";
        try {
            TbGoodsType goodsType = queryRunner.query(sql, new BeanHandler<>(TbGoodsType.class),typeid);
            return goodsType;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

//    @Override
//    public List<TbGoods> selectByGoodsId(Long typeId) {
//        "String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and taz.typeid=?;
//        List<TbGoods> goods =null;
//        try {
//           goods = queryRunner.query(sql, new BeanListHandler<TbGoods>(TbGoods.class), typeId);
//            return  goods;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
